package com.xxx.dorm.web.servlet;

import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.service.BuildingService;
import com.xxx.dorm.service.DormitoryService;
import com.xxx.dorm.service.impl.BuildingServiceImpl;
import com.xxx.dorm.service.impl.DormitoryServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/dormBuild/*")
public class DormBuildServlet extends BaseServlet {

    private BuildingService buildingService = new BuildingServiceImpl();
    private DormitoryService dormitoryService = new DormitoryServiceImpl();

    /**
     * 查找宿舍楼信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findDormBuildList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String conditions = request.getParameter("conditions");
        String information = request.getParameter("information");
        List<Building> buildings = null;
        if(conditions != "" && information != "" && conditions != null && information != null){
            buildings = buildingService.find(conditions,information);
        }else{
            buildings = buildingService.findAll();
        }
        request.setAttribute("buildings",buildings);
        request.setAttribute("mainRight","/WEB-INF/jsp/dorm/dormBuildList.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 查找寝室信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findDormitoryList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String conditions = request.getParameter("conditions");
        String information = request.getParameter("information");
        String number = request.getParameter("number");
        List<Dormitory> dormitories = null;
        if(number != "" && number != null && information != null && information != ""){
            information = information + "#" + number;
            conditions = "buildingname_dormname";
            dormitories = dormitoryService.find(conditions,information);
        }else if(number != "" && number != null && (information == "" || information == null)){
            conditions = "number";
            information = number;
            dormitories = dormitoryService.find(conditions,information);
        }
        else if(conditions != "" && information != "" && conditions != null && information != null){
            dormitories = dormitoryService.find(conditions,information);
        }else{
            dormitories = dormitoryService.findAll();
        }
        for(int i=0;i<dormitories.size();i++){
            Building buildings = buildingService.findByBid(dormitories.get(i).getBid());
            dormitories.get(i).setBuilding(buildings);
        }
        request.setAttribute("dormitories",dormitories);
        request.setAttribute("mainRight","/WEB-INF/jsp/dorm/dormitoryList.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 添加宿舍楼
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void addBuild(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String para = request.getParameter("para");
        if(para != null && "add".equals(para)){
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/addDormBuild.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
            return;
        }
        Map<String, String[]> map = request.getParameterMap();
        Building building = new Building();
        BeanUtils.populate(building,map);
        building.setActualPeople("0");
        building.setCapablePeople("0");
        //判断宿舍楼号是否已经存在
        List<Building> b = buildingService.find("name",building.getName());
        if(b.size()!=0){
            //该宿舍楼的名字已经存在
            request.setAttribute("error","当前宿舍楼名已经存在！");
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/addDormBuild.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
        }else{
            //保存宿舍楼
            buildingService.addBuild(building);
            findDormBuildList(request,response);
        }
    }

    /**
     * 添加寝室
     * @param request
     * @param response
     */
    public void addDormitory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String para = request.getParameter("para");
        if(para != null && "add".equals(para)){
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/addDormitory.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
            return;
        }
        Map<String, String[]> map = request.getParameterMap();
        Integer bid = Integer.valueOf(request.getParameter("dormBuildId"));
        Dormitory dormitory = new Dormitory();
        BeanUtils.populate(dormitory,map);
        dormitory.setActualPeople("0");
        //判断宿舍楼号是否已经存在
        List<Dormitory> d = dormitoryService.find("buildName_number",dormitory.getNumber()+"#"+bid);
        if(d.size()!=0){
            //该宿舍楼的名字已经存在
            request.setAttribute("error","当前寝室已经存在！");
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/addDormitory.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
        }else{
            //保存宿舍楼
            dormitory.setBid(bid);
            dormitoryService.addDormitory(dormitory);
            findDormitoryList(request,response);
        }
    }

    /**
     * 通过id查找宿舍楼信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findOneBuild(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("buildId");
        Building building = buildingService.findByBid(Integer.valueOf(id));
        request.setAttribute("building",building);
        request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/updateDormBuild.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 通过id查找寝室号信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findOneDorm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("dormID");
        Dormitory dormitory = dormitoryService.findByDid(Integer.valueOf(id));
        request.setAttribute("dormitory",dormitory);
        request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/updateDormitory.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
    }

    /**
     * 修改宿舍楼信息（名称）
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void updateBuild(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        Building building = new Building();
        BeanUtils.populate(building,map);
        //判断宿舍楼号是否已经存在
        List<Building> b = buildingService.find("name",building.getName());
        if(b.size()!=0){
            //该宿舍楼的名字已经存在
            request.setAttribute("error","当前宿舍楼名已经存在！");
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/updateDormBuild.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
        }else{
            //修改宿舍楼
            buildingService.updateBuild(building);
            findDormBuildList(request,response);
        }
    }

    public void updateDorm(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Dormitory dormitory = new Dormitory();
        BeanUtils.populate(dormitory,map);
        //判断宿舍楼号是否已经存在
        List<Dormitory> d = dormitoryService.find("buildName_number",dormitory.getNumber()+"#"+dormitory.getBid());
        if(d.size()!=0){
            //该宿舍楼的名字已经存在
            request.setAttribute("error","当前寝室已经存在！");
            request.setAttribute( "mainRight","/WEB-INF/jsp/dorm/updateDormitory.jsp");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
        }else{
            //修改寝室名
            dormitoryService.updateDorm(dormitory);
            findDormitoryList(request,response);
        }
    }

    /**
     * 删除宿舍楼
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void deleteBuild(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String id = request.getParameter("id");
        buildingService.deleteBuild(Integer.valueOf(id));
        findDormBuildList(request,response);
    }

    /**
     * 删除寝室
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void deleteDorm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String id = request.getParameter("id");
        dormitoryService.deleteDorm(Integer.valueOf(id));
        findDormitoryList(request,response);
    }

    /**
     * 查找所有宿舍楼（添加和修改学生时用）
     * @param request
     * @param response
     * @throws IOException
     */
    public void findBuild(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Building> buildings = buildingService.findAll();
        writeValue(buildings,response);
    }

    /**
     * 通过楼号名寻找所有的寝室号
     * @param request
     * @param response
     * @throws IOException
     */
    public void findDormByBuildName(HttpServletRequest request,HttpServletResponse response) throws IOException{
        String BuildName = request.getParameter("BuildName");
        List<Dormitory> dormitories = dormitoryService.findDormByBuildName(BuildName);
        // List<Dormitory> dormitories = dormitoryService.findAll();
        writeValue(dormitories,response);
    }

}
